package persite

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"log"
)

func ItemSaver() chan interface{}{
	out:=make( chan interface{} )
	go func() {
		countItems:=0
		for  {
			item:=<-out
			fmt.Printf("save1 item:%d:%v \n",countItems,item)
			countItems++
			_,err:=save(item)
			if err!=nil {
				log.Printf("save error:%v\n",err)
			}
		}
	}()
	return out
}

func save(item interface{}) (string,error)  {

	client,err:=elastic.NewClient(elastic.SetSniff(false))
	if err!=nil{
		return "",err
	}
	resp,err:=client.Index().
		Index("dating_profile").
		Type("zhenai").
		BodyJson(item).
		Do(context.Background())
	if err!=nil{
		return "",err
	}
	return resp.Id,nil
}
